package com.whlilong.jxc.ui.sorter;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import com.whlilong.jxc.model.Depart;

public class DepartInfoSorter extends ViewerSorter {
	// 每列对应一个不同的常量，正数表示要升序、相反数表示要降序
	private static final int ID = 1;

	private static final int departId = 2;

	private static final int departName = 3;


	// 给外界使用排序器对象

	public static final DepartInfoSorter ID_ASC = new DepartInfoSorter(ID);

	public static final DepartInfoSorter ID_DESC = new DepartInfoSorter(-ID);

	public static final DepartInfoSorter departId_ASC = new DepartInfoSorter(departId);

	public static final DepartInfoSorter departId_DESC = new DepartInfoSorter(-departId);

	public static final DepartInfoSorter departName_ASC = new DepartInfoSorter(departName);

	public static final DepartInfoSorter departName_DESC = new DepartInfoSorter(-departName);


	// 当前所要排序的列，取自上面的ID、NAME两值或其相反数

	private int sortType;

	// 构造函数用private，表示不能在外部创建MySorter对象

	private DepartInfoSorter(int sortType) {

		this.sortType = sortType;

	}

	// 最关键的比较方法compare，改写自ViewerSorter。方法返回值是一个int值：正数则

	// obj1移到obj2之前；零则obj1和obj2的位置不动；负数则obj1移到obj2之后

	public int compare(Viewer viewer, Object obj1, Object obj2) {

		// 传入两条记录（实体类），然后依列给出它们的先后顺序

		Depart o1 = (Depart) obj1;

		Depart o2 = (Depart) obj2;

		switch (sortType) {

		case ID: {

			int l1 = o1.getId();

			int l2 = o2.getId();

			// Long的compareTo方法返回值有三个可能值1,0,-1：

			// 如l1＞l2则返回1；如l1＝l2则返回0；如l1＜l2则返回-1

			return l1 > l2 ? 1 : -1;

		}

		case -ID: {

			int l1 = o1.getId();

			int l2 = o2.getId();

			return l2 > l1 ? 1 : -1;

		}

		case departName: {

			String s1 = o1.getDepartname();

			String s2 = o2.getDepartname();

			return s1.compareTo(s2);

		}

		case -departName: {
			String s1 = o1.getDepartname();
			String s2 = o2.getDepartname();
			return s2.compareTo(s1);
		}
		case departId: {
			String s1 = o1.getDepartid();

			String s2 = o2.getDepartid();

			return s1.compareTo(s2);
		}
		case -departId: {
			String s1 = o1.getDepartid();

			String s2 = o2.getDepartid();

			return s2.compareTo(s1);
		}

		}

		return 0;

	}


}
